package com.gz.mylife.im;

import java.lang.ref.SoftReference;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.ListView;

import com.gz.mylife.Define;
import com.gz.mylife.util.ImageUtils;
import com.gz.mylife.widget.ChattingListView;
import com.gz.mylife.widget.FooterView;
import com.gz.mylife.widget.LoadingCover;
import com.loopj.android.http.RequestParams;
import com.zl.smartmall.library.util.Constants;

public class Result {

	public static final int FIRST = 1;
	public static final int PULL = 2;
	public static final int NEXT = 3;
	
	public static final int FIRST_START = 4;
	public static final int PULL_START = 5;
	public static final int NEXT_START = 6;
	
	
	
	public Result(Activity activity) {
		if(Looper.myLooper() != Looper.getMainLooper()){
			activity.runOnUiThread(new Runnable(){
				@Override
				public void run() {
					mHandler = new RefreshHandler(Result.this);
				}
			});
		} else {
			mHandler = new RefreshHandler(this);
		}
	}
	
	private FooterView mFooterView;
	private LoadingCover mLoadingEmpty;
	private ListView mListView;
	
	public void setFooterView(FooterView footerView) {
		this.mFooterView = footerView;
	}

	public void setLoadingEmpty(LoadingCover loadingEmpty) {
		this.mLoadingEmpty = loadingEmpty;
	}

	public <T extends ListView> void setListView(T listView) {
		this.mListView = listView;
	}

	private Handler mHandler;
	
	/**
	 * 请求返回码
	 */
	public int ret;
	public String msg;
	
	/**
	 * 请求类型 first pull  next
	 */
	private int reqtype = FIRST;
	/**
	 * 消息索引
	 */
	public int lastid;
	/**
	 * 每页返回大小
	 */
	private String pageSize = "20";
	
	
	// 参数
	public String url;
	public Object obj;
	public int arg0;
	public int arg1;
	
	public int getReqtype(){
		return reqtype;
	}
	
	public void first(RequestParams params){
		reqtype = FIRST;
		lastid = 0;
		ret = -1;
		params.put("flag", "0");
		params.put("lastid", "0");
		params.put("limit", pageSize);
	}
	
	public void next(RequestParams params){
		reqtype = NEXT;
		ret = -1;
		params.put("flag", "1");
		params.put("lastid", lastid+"");
		params.put("limit", pageSize);
	}
	
	public void pull(RequestParams params){
		reqtype = PULL;
		ret = -1;
		params.put("flag", "0"); //方向
		params.put("lastid", "0");
		params.put("limit", pageSize);
	}
	
	public void parseResult(JSONObject response){
		try {
			ret = response.getInt("ret");
			msg = response.getString("msg");
			
			JSONArray array = null;
			if(response.get("result") instanceof JSONObject){
				JSONObject result = response.getJSONObject("result");
				if(result.has("data")){
					array = result.getJSONArray("data");
				}
			} else if (response.get("result") instanceof JSONArray){
				response.getJSONArray("result");
			}
			if(array!= null && array.length() > 0){
				JSONObject lastItem = array.getJSONObject(array.length()-1);
				if(lastItem.has("lastid")){
					lastid = lastItem.getInt("lastid");
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
	public void start(){
		if(reqtype == FIRST){
			mHandler.sendEmptyMessage(FIRST_START);
		} else if(reqtype == NEXT){
			mHandler.sendEmptyMessage(NEXT_START);
		} 
	}
	
	public void finish(){
		if(mHandler == null)
			return;
		if(reqtype == FIRST){
			mHandler.sendEmptyMessage(FIRST);
		} else if(reqtype == NEXT){
			mHandler.sendEmptyMessage(NEXT);
		} else if(reqtype == PULL){
			mHandler.sendEmptyMessage(PULL);
		} 
	}
	
	static class RefreshHandler extends Handler{
		private SoftReference<Result> ref;
		
		public RefreshHandler(Result target) {
			ref = new SoftReference<Result>(target);
		}
		@Override
		public void handleMessage(Message msg) {
			Result result = ref.get();
			if(result == null) return;
			switch(msg.what){
			case FIRST_START:
				if(result.mLoadingEmpty!= null){
					result.mLoadingEmpty.setVisibility(View.VISIBLE);
				}
				break;
			case NEXT_START:
				if(result.mFooterView!= null){
//					result.mFooterView.startLoading();
				}
				break;
			case PULL_START:
				//TODO
				break;
			case FIRST:
				if(result.mLoadingEmpty!= null){
					if(result.ret == 0){
						result.mLoadingEmpty.onRefreshSuccess();
						
					} else if(result.ret == Constants.REQ_NO_MORE_DATA){
						result.mLoadingEmpty.onRefreshFailed(Define.MSG_NOTHING);
					} else {
						result.mLoadingEmpty.onRefreshFailed();
					}
				}
				break;
			case NEXT:
				if(result.mFooterView!= null){
					if(result.ret == 0){
						if(result.hasNext()){
							result.mFooterView.onRefreshSuccess();
						} else {
							result.mFooterView.onRefreshComplete();
						}
					} else if(result.ret == Define.REQ_NO_MORE_DATA){
						result.mFooterView.onRefreshComplete();
					} else {
						result.mFooterView.onRefreshFailed();
					}
				}
				break;
			case PULL:
				if(result.mListView!= null){
					if(result.ret == 0 || result.ret == Define.REQ_NO_MORE_DATA){
//						result.mListView.onRefreshSuccess();
						if(result.mListView.getClass() == ChattingListView.class){
							((ChattingListView) result.mListView).onRefreshComplete();
						}else {
//							((PullToRefreshListView) result.mListView).getLoadingLayoutProxy().refreshSuccess();
						}
						
					} else {
//						result.mListView.onRefreshFail();
						if(result.mListView.getClass() == ChattingListView.class){
							((ChattingListView) result.mListView).onRefreshComplete();
						}else {
//							((PullToRefreshListView) result.mListView).getLoadingLayoutProxy().refreshFailed();
						}
					}
//					sendEmptyMessageDelayed(100, 1000);
				}
				break;
//			case 100:
//				result.mListView.onRefreshComplete();
//				break;
			}
		}
	}
	
	public boolean hasNext(){
		if(ret == Define.REQ_NO_MORE_DATA){
			return false;
		} else {
			return true;
		}
	}
	
//	public void initView(Activity activity){
//		if(Looper.myLooper() != Looper.getMainLooper()){
//			activity.runOnUiThread(new Runnable(){
//				@Override
//				public void run() {
//					mHandler = new RefreshHandler(Result.this);
//				}
//			});
//		} else {
//			mHandler = new RefreshHandler(this);
//		}
//		mListView= (PullToRefreshListView) activity.findViewById(R.id.listview1);
//		mLoadingEmpty = (LoadingEmpty) activity.findViewById(R.id.layout_loading_empty);
//		mFooterView = (FooterView) activity.getLayoutInflater().inflate(R.layout.footer_view, null);
//	}
	
	public void destory(){
		if(mHandler != null){
			mHandler.removeMessages(FIRST);
			mHandler.removeMessages(NEXT);
			mHandler.removeMessages(PULL);
			mHandler = null;
			mFooterView = null;
			mLoadingEmpty = null;
			ImageUtils.unbindDrawables(mListView);
			mListView = null;
		}
	}
}
